﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Core.Framework.Distributed.Model
{
    /// <summary>
    /// 申请锁上下文
    /// </summary>
    public class ApplyLockContext
    {

        public ApplyLockContext()
        {
            this.StartDate = DateTime.Now;
            this.WaitHandle = new AutoResetEvent(false);
        }

        /// <summary>
        /// 申请Token
        /// </summary>
        public string ApplyToken { get; set; }

        /// <summary>
        /// 是否超时取消
        /// </summary>
        public bool? IsCancel { get; set; }

        /// <summary>
        /// 是否申请锁成功
        /// </summary>
        public bool? IsApplySuccess { get; set; }

        /// <summary>
        /// 是否完成任务
        /// </summary>
        public bool? IsTaskComplete { get; set; }

        /// <summary>
        /// 开始时间
        /// </summary>
        internal DateTime StartDate { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        internal DateTime EndDate { get; set; }


        /// <summary>
        /// 超时处理线程
        /// </summary>
        public Thread ApplyExpirationThread { get; set; }


        /// <summary>
        /// 线程等待事件
        /// </summary>
        public EventWaitHandle WaitHandle { get; set; }

        /// <summary>
        /// 申请实体
        /// </summary>
        public ApplyLockWaitEntity ApplyEntity { get; set; }

        /// <summary>
        /// 任务字典
        /// </summary>
        public TaskDicEntity TaskEntity { get; set; }


        /// <summary>
        /// 任务用时
        /// </summary>
        public double GetTaskDateTotalMilliseconds()
        {
            TimeSpan ts1 = new TimeSpan(StartDate.Ticks);
            TimeSpan ts2 = new TimeSpan(EndDate.Ticks);
            TimeSpan ts3 = ts1.Subtract(ts2).Duration();
            return ts3.TotalMilliseconds;
        }


    }
}
